import time
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from commom.Utils import WebChatDriver

#测试登录界面

class WebChatLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:8080/login.html"
        self.driver=WebChatDriver.driver
        self.driver.get(self.url)

    def CheckElem(self):

        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.nav").text
        assert text=="网页聊天"
        self.driver.find_element(By.CSS_SELECTOR, "#username")
        self.driver.find_element(By.CSS_SELECTOR, "#password")
        self.driver.find_element(By.CSS_SELECTOR, "#submit")


    #成功登录的样例
    def LoginSuccTest(self):
        print("----登录成功测试-----")
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        #切换到弹窗
        wait = WebDriverWait(self.driver, 5)
        # 等待直到页面出现警告框，满足这个条件才会继续往下执行
        alert = wait.until(EC.alert_is_present())
        alert=self.driver.switch_to.alert
        text=alert.text
        alert.accept()
        assert text=="登录成功!"

        #检查到指定元素就代表登录成功了
        self.driver.find_element(By.CSS_SELECTOR, "body > div.client-container > div > div.left > div.user")
        assert "网页聊天程序" ==self.driver.title
        WebChatDriver.getScreeShot()
        print("-----登录成功测试-----[pass]\n")


    #登录失败测试,错误密码
    def LoginFailTest(self):
        print("----登录失败测试----")
        print("***测试输入账号密码为空***")
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait = WebDriverWait(self.driver, 5)
        # 等待直到页面出现警告框，满足这个条件才会继续往下执行
        wait.until(EC.alert_is_present())
        # 切换到弹窗
        alert=self.driver.switch_to.alert

        text=alert.text
        alert.accept()

        assert "当前输入的用户名或者密码为空!" == text
        print("***测试输入账号密码为空[pass]***\n")

        print("***测试输入账号正确密码错误***")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()


        wait = WebDriverWait(self.driver, 5)
        # 等待直到页面出现警告框，满足这个条件才会继续往下执行
        wait.until(EC.alert_is_present())
        # 切换到弹窗
        alert=self.driver.switch_to.alert

        text=alert.text
        alert.accept()

        assert "登录失败!" == text
        print("***测试输入账号正确密码错误[pass]***\n")

        print("***测试输入账号错误密码正确***")
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi123")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait = WebDriverWait(self.driver, 5)
        # 等待直到页面出现警告框，满足这个条件才会继续往下执行
        wait.until(EC.alert_is_present())
        # 切换到弹窗
        alert=self.driver.switch_to.alert

        text=alert.text
        alert.accept()
        assert "登录失败!" == text

        print("***测试输入账号错误密码正确[pass]***\n")

        WebChatDriver.getScreeShot()
        print("----登录失败测试----[pass]\n")


# login=WebChatLogin()
# login.CheckElem()
# login.LoginSuccTest()
# login.LoginFailTest()


